local SceneBase = require("app.Base.SceneBase")
local MapLayer = require("app.layer.MapLayer"):GetInstance()
local ActorManage = require("app.manage.ActorManage"):GetInstance()

local LatticeScene = class("LatticeScene", SceneBase)

function LatticeScene:ctor()
    print("LatticeScene")
    LatticeScene.super.ctor(self,data)    
    self:initUI()

    self.Contro = { Play = false , Stepping = false }  
    self.Actor={}
    
    self:SetCenter()   
end

function LatticeScene:initUI()
    self.bg = display.newSprite("UI/bg/bg.jpg"):addTo(self):move(display.cx,display.cy)
    self.MapLayer = MapLayer:addTo(self)    

    self.RightNode = display.newNode()
    self.RightNode:move(850,display.cy):addTo(self)    

    self:ControlUI()
end

function LatticeScene:ControlUI() 
    self.Play_btn = td.CreatBtn({text={str="开始"},cb = handler(self,self.ClickControPlay)}):addTo(self.RightNode):move(-100,300)

    self.Stepping_btn = td.CreatBtn({text={str="步进"},cb = handler(self,self.ClickControStepping)}):addTo(self.RightNode):move(100,300)

    self.Center_btn = td.CreatBtn({text={str="设为中心点"},cb = handler(self,self.SetCenter)}):addTo(self.RightNode):move(-100,200)

    self.Center_x = td.CreateEditBox({text = "7" ,placeHolder = "X" }):move(25,200):addTo(self.RightNode)
    self.Center_y = td.CreateEditBox({text = "7" ,placeHolder = "Y" }):move(125,200):addTo(self.RightNode)

    self.Add_btn = td.CreatBtn({text={str="添加点"},cb = handler(self,self.AddNode)}):addTo(self.RightNode):move(-100,100)

    self.Add_x = td.CreateEditBox({text = "4" ,placeHolder = "X" }):move(25,100):addTo(self.RightNode)
    self.Add_y = td.CreateEditBox({text = "4" ,placeHolder = "Y" }):move(125,100):addTo(self.RightNode)

    self.Delete_btn = td.CreatBtn({text={str="删除点"},cb = handler(self,self.DeleteNode)}):addTo(self.RightNode):move(-100,0)

    self.Delete_id = td.CreateEditBox({text="2",placeHolder = "ID" }):move(50,0):addTo(self.RightNode)

end

function LatticeScene:DeleteNode()
    local Delete_id = tonumber(self.Delete_id:getText())
    if Delete_id then 
        local _actor = ActorManage:GetActor(Delete_id)
        self.MapLayer:removeChild(_actor)
        ActorManage:DeleteActor(Delete_id)  
        self.Delete_id:setText(Delete_id+1)
    end
end

function LatticeScene:AddNode()
     local Grid_x = tonumber(self.Add_x:getText())
     local Grid_y = tonumber(self.Add_y:getText())
--     print("添加到点"..Grid_x..Grid_y)
     local pos = td.GetPosForGrid({gridX = Grid_x,gridY = Grid_y})
--     if self.Actor then         
--        self.Actor:move(pos)
--     else
        local Actor = ActorManage:newActor(2001,{color = cc.c4b(100,100,255,255) , pos = pos})
        Actor:addTo(self.MapLayer)
        table.insert(self.Actor,Actor.id,Actor)
--     end
end

function LatticeScene:SetCenter()
    local Grid_x = tonumber(self.Center_x:getText())
    local Grid_y = tonumber(self.Center_y:getText())
--     print("设为中心点"..Center_x..Center_y)
    local pos = td.GetPosForGrid( { gridX = Grid_x, gridY = Grid_y })
    if self.CenterNode then
        self.CenterNode:move(pos)
    else
        self.CenterNode = ActorManage:newActor(3001,{color = cc.c4b(255,100,100,255) , pos = pos})
        self.CenterNode:addTo(self.MapLayer)
        td.CenterNode = self.CenterNode
    end
end

function LatticeScene:ClickControPlay()
    self.Contro.Play = not self.Contro.Play
    if self.Contro.Play then 
        td.BtnAddText(self.Play_btn,{str="暂停"})
    else 
        td.BtnAddText(self.Play_btn,{str="继续"})
    end
end

function LatticeScene:ClickControStepping()
    self.Contro.Play = false
    td.BtnAddText(self.Play_btn,{str="继续"})
    self.Contro.Stepping = true
end

function LatticeScene:Update(dt)
    if self.Contro.Play or self.Contro.Stepping then 
        if self.Contro.Stepping then self.Contro.Stepping = false end
        MapLayer:Update(dt)
        ActorManage:Update(dt)
    end 
end

return LatticeScene